package com.zdzx.controller.web;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.base.common.util.RedisUtil;
import com.zdzx.base.common.util.UserInfoUtil;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.EnterpriseRecordsMapper;
import com.zdzx.mapper.UserMapper;
import com.zdzx.model.service.base.result.UserInfo;
import com.zdzx.model.service.base.vo.User;
import com.zdzx.model.service.content.param.EnterpriseAuth;
import com.zdzx.model.service.content.vo.EnterpriseRecords;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

	
@Api(tags = "web企业档案")
@RestController()
@RequestMapping("/web")
public class WebEnterpriseRecordsController{
	
	@Autowired
	private EnterpriseRecordsMapper enterpriseRecordsMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RedisUtil redisUtil;
	
	
	
	@ApiOperation(value = "获取列表", notes = "") 
	@GetMapping("/enterpriseRecords") 
	public Result<PageInfo<EnterpriseRecords>> list(
			@ApiParam("") @RequestParam(value = "pageNum", required = false)Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false)Integer pageSize,
			@ApiParam("") @RequestParam(value = "realName", required = false)String realName){
		if(pageNum==null||pageSize==null) {
			pageNum=1;
			pageSize=10;
		}
		QueryWrapper<EnterpriseRecords> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		if(!StringUtils.isEmpty(realName)) {
			queryWrapper.eq("real_name", realName);
		}
		Page<EnterpriseRecords> page=new Page<>(pageNum, pageSize);
		 IPage<EnterpriseRecords> info = enterpriseRecordsMapper.selectPage(page, queryWrapper);
		 PageInfo<EnterpriseRecords> data = PageUtil.data(info.getPages(),info.getCurrent(),info.getTotal(),info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,data);
	}

	
	
	
	
	
	@ApiOperation(value = "添加", notes = "") 
	@PostMapping("/enterpriseRecords") 
	public Result<Integer> save(@RequestBody EnterpriseRecords entity){
		entity.setId(IdWorker.getIdStr());
		entity.setCreateTime(LocalDateTime.now());
		 int rows = enterpriseRecordsMapper.insert(entity);
		if(rows>0){
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),null,null);
		}
	
	}
	
	
	@ApiOperation(value = "修改", notes = "") 
	@PutMapping("/enterpriseRecords") 
	public Result<Integer> edit(@RequestBody EnterpriseRecords entity){
		entity.setUpdateTime(LocalDateTime.now());
		int rows = enterpriseRecordsMapper.updateById(entity);
		if(rows>0){
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),ResultCode.RESULT_ERROR.getCode(),null);
		}
	
	}
	
	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/enterpriseRecords/{id}") 
	public Result<Integer> del(@PathVariable("id")String id){
			int rows = enterpriseRecordsMapper.deleteById(id);
			if(rows>0){
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
			}else {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),ResultCode.RESULT_ERROR.getCode(),null);
			}
		
	}
	
	@ApiOperation(value = "企业证人", notes = "") 
	@PostMapping("/enterprise/auth") 
	public Result<Integer> enterpriseAuth(@RequestBody EnterpriseAuth enterpriseAuth,HttpServletRequest request){
		if(StringUtils.isEmpty(enterpriseAuth.getEnterpriseId())||"00000000".equals(enterpriseAuth.getEnterpriseId())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"企业不正确！！！" , null);
		}
		if(StringUtils.isEmpty(enterpriseAuth.getRealName())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"姓名不能为空！！！" , null);
		}
		if(StringUtils.isEmpty(enterpriseAuth.getIdCard())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"身份证不能为空！！！" , null);
		}
		if(StringUtils.isEmpty(enterpriseAuth.getRoleType())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"认证身份不能为空！！！" , null);
		}
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if(loginInfo==null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(),ResultCode.NOT_LOGIN.getMes() , null);
		}
		if(!StringUtils.isEmpty(loginInfo.getIdCard())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"已认证！！！" , null);
		}
		User user=new User();
		user.setId(loginInfo.getId());
		user.setRoleType(enterpriseAuth.getRoleType());
		user.setRealName(enterpriseAuth.getRealName());
		user.setIdCard(enterpriseAuth.getIdCard());
		user.setTenantId(enterpriseAuth.getEnterpriseId());
		int rows=userMapper.updateById(user);
		if(rows>0){
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
			}else {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),ResultCode.RESULT_ERROR.getCode(),null);
			}
		
	}
}
